<?php  
 
class Teacher extends Model {
// ������

      private  $teachers_id = false;
      private    $name = false;
      private    $second_name = false;
      private    $surname = false;
      private    $email = false;
      private    $phone = false;
      private    $small_info = false;
      private    $info = false;
      private    $extra_info = false;

    function __construct($params = null){ 
       
       
        
       
        if(isset($params['teachers_id'])) $this->teachers_id = $params['teachers_id'];
        if(isset($params['second_name'])) $this->second_name = $params['second_name'];
        if(isset($params['name'])) $this->name = $params['name'];
        if(isset($params['surname'])) $this->surname = $params['surname'];
        if(isset($params['email'])) $this->email = $params['email'];
        if(isset($params['phone'])) $this->phone = $params['phone'];
        if(isset($params['small_info'])) $this->small_info = $params['small_info'];
        if(isset($params['info'])) $this->info = $params['info'];
        if(isset($params['extra_info'])) $this->extra_info = $params['extra_info'];
             
      
    }
    
    function getEditTeacherLink($teachers_id){
    //TODO seo link generator
        //$teacher = $this->getTeacher($teachers_id);
    
        $link = '/Teacher/EditTeacher/' . $teachers_id;
        
        return $link;
    } // --  

    function getTeacher($teachers_id){
        $q = "SELECT * FROM teachers WHERE teachers_id = $teachers_id ";
        $r = mysql_query($q);
        if(!$r) return -1;
        if( mysql_num_rows($r) > 0){
          $tmp = mysql_fetch_assoc($r);
          return $tmp;
        }
        return false;
    } // --
    
     function getTeachers(){
        $q = "SELECT * FROM teachers";
        $r = mysql_query($q);
        if(!$r) return -1;
       if( mysql_num_rows($r) > 0){
          while($tmp = mysql_fetch_assoc($r)){
              if($tmp['photo'] != '')
                $tmp['photo'] = explode(';',$tmp['photo']);
                //$this->checkUpImg($tmp['photo']);  
              $tmp['link'] = '/Teacher/ShowTeacher/' . $tmp['teachers_id'];  
              $arr[] = $tmp;   
          }    return $arr;
        } 
        return false;
     } // --
     
     function checkUpImg($img_arr, $id){ // prototype (���������� �� ��������. �������, ���� ���)
        $prefix = "../Image/teacher/";
        
        for($i=0; $i<count($img_arr); $i++)
          if(!is_file($_SERVER['DOCUMENT_ROOT'] . $prefix . $img_arr[$i])){
            $this->updateTeacher($teacher); // [!]
        } 
        
     } // --
     
     
     function deleteTeacher($teachers_id){
               
          $teacher = $this->getTeacher($teachers_id);    
          if( preg_match('/\.jpg$/i',$teacher['photo']) ){
            //preg_replace('/\.jpg$/i','',$teacher['photo']);  fb($teacher['photo']);
            $pos = strpos($teacher['photo'], '.jpg');            
            $prefix = substr($teacher['photo'],0,$pos);
            
          }
          else $prefix = $teacher['photo'];
          
          $file_1 = $prefix . '.jpg';
          $file_2 = $prefix . '_sm.jpg';
              
          $this->delTeacher(array('id'=>$teachers_id, 'photo'=>array($file_1,$file_2)));
          
          //unlink();
     } // --
     
     function delTeacher($teacher){
     // [call] $this->delTeacher(array('id'=>$tmp['teachers_id'] ));
      
        if(isset($teacher['id']) OR isset($teacher['teachers_id'])){ // by ID
            if(isset($teacher['id'])) $teachers_id =  $teacher['id'];
            elseif(isset($teacher['teachers_id'])) $teachers_id =  $teacher['teachers_id'];
        
            $q = "DELETE FROM `teachers` WHERE teachers_id = $teachers_id";
            $r = mysql_query($q);
            //TODO ������� ��������� ������
            //if($r) return true;
            //return -1;
        }
        
        if(isset($teacher['photo'])){
            if(is_array($teacher['photo'])){
              foreach($teacher['photo'] as $file){
                  @unlink(SITE . '../Image/teacher/' . $file);
              }
            }
            else {
              @unlink(SITE . '../Image/teacher/' . $teacher['photo']);
            }
        }
          
     } // --
    
     function updateTeacher($teacher){ // proto
        if(isset($teacher['id']) OR isset($teacher['teachers_id'])){ // update by ID
            if(isset($teacher['id'])) $teachers_id =  $teacher['id'];
            elseif(isset($teacher['teachers_id'])) $teachers_id =  $teacher['teachers_id'];
            
            
            // ...
        
            $q = "UPDATE teachers   WHERE teachers_id = $teachers_id";
            $r = mysql_query($q);
            if($r) return true;
            return -1;
        }
     } // --
}

    
 
?>